package com.mooer.utils.ui;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;



public class ImageUtils {
	
	/***
	 * 压缩图片
	 * @param bmp
	 * @param scale 设置缩放比例 或者根据新的size来计算出缩放比例
	 * @return
	 */
	public static Bitmap compress(Bitmap bmp, double scale)
	{
	     //获得原始图片的size
	     int bmpWidth=bmp.getWidth();
	     int bmpHeight=bmp.getHeight();

	     //设置缩放比例 或者根据新的size来计算出缩放比例
//	     double scale=1.25;

	     //计算出这次要缩小的比例
	     float scaleWidth=(float)(bmpWidth*scale);
	     float scaleHeight=(float)(bmpHeight*scale);

	     //产生resize后的Bitmap对象
	     Matrix matrix=new Matrix();
	     matrix.postScale(scaleWidth, scaleHeight);
//	     matrix.postRotate(45);//方向变换
	     //得到新的bitmap
	     return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
	    
	}
	
	/***
	 * 压缩放大图片
	 * @param bitmap
	 * @param w
	 * @param h
	 * @return
	 */
	public static Bitmap resizeImage(Bitmap bitmap, int w, int h) 
	{
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		float scaleWidth = ((float) w) / width;
		float scaleHeight = ((float) h) / height;

		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		
		return Bitmap.createBitmap(bitmap, 0, 0, width,	height, matrix, true);
	}
	
	/**
	  * 图片圆角
	  * @param bitmap
	  * @param roundPx
	  * @return
	  */
	 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
		 if(bitmap == null){
			 return null;
		 }
	     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
	         bitmap.getHeight(), Config.ARGB_8888);
	     Canvas canvas = new Canvas(output);

	     final int color = 0xff424242;
	     Paint paint = new Paint();
	     Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
	     RectF rectF = new RectF(rect);
	     if(roundPx <= 0)
	    	 roundPx = 12;

	     paint.setAntiAlias(true);
	     canvas.drawARGB(0, 0, 0, 0);
	     paint.setColor(color);
	     canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

	     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	     canvas.drawBitmap(bitmap, rect, rect, paint);
	     paint = null; rect = null; rectF = null; canvas = null;
	     return output;
//		 return bitmap;
	 }
	 
		public static Bitmap drawableToBitmap(Drawable drawable) {

			Bitmap bitmap = Bitmap

			.createBitmap(

			drawable.getIntrinsicWidth(),

			drawable.getIntrinsicHeight(),

			drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

			: Bitmap.Config.RGB_565);

			Canvas canvas = new Canvas(bitmap);

			//canvas.setBitmap(bitmap);

			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

			drawable.draw(canvas);

			return bitmap;

			}
		


}
